package com.gistone.commis.api.restController;

import com.gistone.commis.constant.Status;
import com.gistone.commis.dto.Result;
import com.gistone.commis.entity.Fund;
import com.gistone.commis.entity.User;
import com.gistone.commis.mapper.*;
import com.gistone.commis.util.ResponseHeaderUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.*;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by wangfan on 2017/6/11.
 */

@RestController("apiDownloadController")
@RequestMapping("api/download")
public class DownloadController {
    private static final Logger logger = LoggerFactory.getLogger(DownloadController.class);
    @Value("${app.uploadPath}")
    private String path;


    @Autowired
    private FundMapper fundMapper;


    @Autowired
    private OfficeSuppliesMapper officeSuppliesMapper;


    @Autowired
    private AttendanceMapper attendanceMapper;

    @Autowired
    private OvertimeMapper overtimeMapper;


    @Autowired
    private UserMapper userMapper;

    @Autowired
    private PersonMapper personMapper;


    @RequestMapping("/file")
    public void file(@RequestParam Integer id,@RequestParam String  type,@RequestParam String  filename, HttpServletRequest request, HttpServletResponse response) throws Exception {

        String uploadPath =path+"/"+type+"/"+id+"/";

        File file = new File(uploadPath+filename);
        if  (!file .exists()  && !file .isDirectory())
        {
            return;
        }
        System.out.println("------"+filename);
        ResponseHeaderUtil.setResponseHeader(response, filename);
        response.setContentLength((int) file.length());
        InputStream inputStream = new BufferedInputStream(new FileInputStream(file));
        FileCopyUtils.copy(inputStream, response.getOutputStream());

    }


}
